package com.zhs.gccs.fragment.main;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.annimon.stream.Optional;
import com.annimon.stream.Stream;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.shape.view.ShapeTextView;
import com.zhs.gccs.R;
import com.zhs.gccs.activity.DrawerMainActivity;
import com.zhs.gccs.activity.LoginActivity;
import com.zhs.gccs.activity.gccs.AdminGccsTaskActivity;
import com.zhs.gccs.activity.gccs.AdminPersonActivity;
import com.zhs.gccs.activity.gccs.GccsUserTaskListActivity;
import com.zhs.gccs.fragment.BaseFragment;
import com.zhs.gccs.model.SysRole;
import com.zhs.gccs.model.WorkTableMenu;
import com.zhs.gccs.test.ComposeActivity;
import com.zhs.gccs.utils.OnNoDoubleClickListener;
import com.zhs.gccs.utils.SharedPrefHelper;
import com.zhs.gccs.utils.ToastUtils;
import com.zhs.gccs.view.WorkMenuView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
//工作台
public class WorkTableFragment extends BaseFragment {
    @BindView(R.id.rl_title)
    LinearLayout rl_title;
    @BindView(R.id.tv_header)
    ShapeTextView tv_header;
    @BindView(R.id.menu_todo)
    WorkMenuView menu_todo;
    private List<WorkTableMenu> todoMenus;
    @BindView(R.id.menu_task)
    WorkMenuView menu_task;
    private List<WorkTableMenu> taskMenus;
    @BindView(R.id.menu_query)
    WorkMenuView menu_query;
    private List<WorkTableMenu> queryMenus;


    public static WorkTableFragment newInstance() {
        Bundle args = new Bundle();
        WorkTableFragment fragment = new WorkTableFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    protected int getLayout() {
        return R.layout.fragment_worktable;
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//        ImmersionBar.with(getActivity())
//                .statusBarColor(R.color.main_title_pressed_bg).init();
        ImmersionBar.with(getActivity())
                .statusBarColor(R.color.white).init();
        rl_title.setPadding(0, ImmersionBar.getStatusBarHeight(this), 0, 0);
        String userNickName = SharedPrefHelper.getInstance().getUserNickName();
        if(TextUtils.isEmpty(userNickName)){
            userNickName = SharedPrefHelper.getInstance().getUserAccount();
        }
        if(!TextUtils.isEmpty(userNickName)){
            tv_header.setText(userNickName.substring(0,1));
        }else {
            tv_header.setText("");
        }
        tv_header.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                DrawerMainActivity activity = (DrawerMainActivity) getActivity();
                activity.openDrawer();
            }
        });
        List<String> roles = SharedPrefHelper.getInstance().getUserRoles();
        if(roles==null||roles.size()==0){
            ToastUtils.toast("无权限操作，请联系管理员分配权限后重新登录！");
            toLogin();
            return;
        }
        //审批功能
        menu_todo.setTitle("待办审批");
        todoMenus = new ArrayList<>();
        //核查功能
        menu_task.setTitle("抽检任务");
        taskMenus = new ArrayList<>();
        String gccsAdmin = Stream.of(roles)
                .filter(i -> SysRole.role_gccs_admin.equals(i)).findFirstOrElse(null);
        if(!TextUtils.isEmpty(gccsAdmin)){
            todoMenus.add(new WorkTableMenu("人员管理","去添加",
                    R.mipmap.icon_rygl,"#ffffff",
                    AdminPersonActivity.class));//
            taskMenus.add(new WorkTableMenu("任务下发","去下发",
                    R.mipmap.icon_rwxf,"#ffffff", AdminGccsTaskActivity.class));
        }
        String gccsCjr = Stream.of(roles)
                .filter(i -> SysRole.role_gccscjr.equals(i)).findFirstOrElse(null);
        if(!TextUtils.isEmpty(gccsCjr)){
            taskMenus.add(new WorkTableMenu("工程采集","去填报",
                    R.mipmap.icon_gccs,"#ffffff", GccsUserTaskListActivity.class));
        }
        //查询功能
        queryMenus = new ArrayList<>();
        menu_todo.setMenus(todoMenus);
        if (todoMenus.size()>0) {
            menu_todo.setVisibility(View.VISIBLE);
        }else {
            menu_todo.setVisibility(View.GONE);
        }
        menu_task.setMenus(taskMenus);
        if (taskMenus.size()>0) {
            menu_task.setVisibility(View.VISIBLE);
        }else {
            menu_task.setVisibility(View.GONE);
        }

        menu_query.setMenus(queryMenus);
        if (queryMenus.size()>0) {
            menu_query.setVisibility(View.VISIBLE);
        }else {
            menu_query.setVisibility(View.GONE);
        }
    }
    void toLogin() {
        Intent intent = new Intent(context, LoginActivity.class);
        startActivity(intent);
    }
}
